package com.roc.steelend.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.account.AccountSpecialKaipingOrderIdDetail;
import com.roc.steelend.controller.vo.account.AccountSpecialKaipingReception;
import com.roc.steelend.controller.vo.invoice.InvoiceByKaipingOrder;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.specialInvoice.SpecialInvoiceByKaipingReception;
import com.roc.steelend.controller.vo.specialPay.SpecialByKaipingReception;
import com.roc.steelend.mapper.*;
import com.roc.steelend.pojo.Account;
import com.roc.steelend.pojo.CompanyMoney;
import com.roc.steelend.pojo.Invoice;
import com.roc.steelend.pojo.KaipingGuy;
import com.roc.steelend.service.AccountService;
import com.roc.steelend.service.InvoiceService;
import com.roc.steelend.service.KaipingGuyService;
import com.roc.steelend.util.LshUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.annotations.ApiIgnore;

import java.awt.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.*;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class KaipingGuyServiceImpl  implements KaipingGuyService {
    @Autowired
    private KaipingGuyMapper kaipingGuyMapper;

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private CompanyMoneyMapper companyMoneyMapper;
    @Autowired
    private KaipingOrderMapper kaipingOrderMapper;
    @Autowired
    private AccountService accountService;
    @Autowired
    private InvoiceService invoiceService;
    @Autowired
    private InvoiceMapper invoiceMapper;

    @Override
    public List<KaipingGuy> findKaipingGuyList(String searchMsg, Integer now, Integer size) {
        QueryWrapper<KaipingGuy> wrapper = new QueryWrapper<>();
        if (searchMsg!=null){
            wrapper.like("kaipingguy_name",searchMsg);
        }
        now=(now-1)*size;
        wrapper.last("LIMIT "+now+" , "+size);
        return kaipingGuyMapper.selectList(wrapper);
    }

    @Override
    public Integer countKaipingGuy(String searchMsg) {
        QueryWrapper<KaipingGuy> wrapper = new QueryWrapper<>();
        if (searchMsg!=null){

            wrapper.like("kaipingguy_name",searchMsg);
        }
        return kaipingGuyMapper.selectCount(wrapper);
    }

    @Override
    public KaipingGuy getKaipingGuyData(Integer kaipingguyId) {
        QueryWrapper<KaipingGuy> wrapper = new QueryWrapper<>();

        wrapper.eq("kaipingguy_id",kaipingguyId);

        return kaipingGuyMapper.selectList(wrapper).get(0);
    }

    @Override
    public ReturnMsg postAccountKaiingGuy(SpecialByKaipingReception specialByKaipingReception, Integer employeeId) {
        ReturnMsg msg = new ReturnMsg();

        List<HashMap<String, Object>> hashMaps = kaipingOrderMapper.selectAllBuKaipingguyId(specialByKaipingReception.getKaipingguyId());
        if (hashMaps.isEmpty()){
            Account account = new Account();
            account.setAccountId(LshUtil.getAccountSpecialLsh());
            account.setEmployeeId(employeeId);
            account.setPayTakeId(specialByKaipingReception.getPayOrTakeWay());
            account.setSmallAccountId(8);
            account.setOperateTime(new Timestamp(new Date().getTime()));
            account.setAccountTime(specialByKaipingReception.getPayTime());
            account.setMoney(specialByKaipingReception.getPayMoney());
            account.setWho(specialByKaipingReception.getKaipingguyName());
            if (Objects.equals(specialByKaipingReception.getRemark(), "")){
                specialByKaipingReception.setRemark(null);
            }
            account.setRemark(specialByKaipingReception.getRemark());
            account.setWay(1);
            int insert = accountMapper.insert(account);

            QueryWrapper<KaipingGuy> kaipingGuyQueryWrapper = new QueryWrapper<>();
            kaipingGuyQueryWrapper.eq("kaipingguy_id",specialByKaipingReception.getKaipingguyId());
            KaipingGuy kaipingGuy = kaipingGuyMapper.selectList(kaipingGuyQueryWrapper).get(0);
            if (specialByKaipingReception.getIsOver()){
                kaipingGuy.setDebt(new BigDecimal("0"));
            }else {
                kaipingGuy.setDebt(kaipingGuy.getDebt().subtract(specialByKaipingReception.getPayMoney()));
            }
            int i = kaipingGuyMapper.updateById(kaipingGuy);
            QueryWrapper<CompanyMoney> companyMoneyQueryWrapper = new QueryWrapper<>();
            companyMoneyQueryWrapper.eq("pay_take_id",specialByKaipingReception.getPayOrTakeWay());
            CompanyMoney companyMoney = companyMoneyMapper.selectList(companyMoneyQueryWrapper).get(0);
            companyMoney.setMoney(companyMoney.getMoney().subtract(specialByKaipingReception.getPayMoney()));
            int i1 = companyMoneyMapper.updateById(companyMoney);

            if (insert==1&&i==1&&i1==1){
                msg.setCode(1);
                msg.setMsg("开平付款账务操作成功");

            }else {
                msg.setCode(0);
                msg.setMsg("开平付款账务操作失败");
            }
            return msg;
        }else {
            ArrayList<AccountSpecialKaipingOrderIdDetail> accountSpecialKaipingOrderIdDetails = new ArrayList<>();
            for (int i = 0; i <hashMaps.size() ; i++) {
                HashMap<String, Object> hashMap = hashMaps.get(i);
                String kaiping_order_id = hashMap.get("kaiping_order_id").toString();
                BigInteger bigInteger = new BigInteger(kaiping_order_id);
                java.sql.Date processing_time = (java.sql.Date)hashMap.get("processing_time");
                AccountSpecialKaipingOrderIdDetail accountSpecialKaipingOrderIdDetail = new AccountSpecialKaipingOrderIdDetail(bigInteger, processing_time);
                accountSpecialKaipingOrderIdDetails.add(accountSpecialKaipingOrderIdDetail);
            }
            AccountSpecialKaipingReception accountSpecialKaipingReception = new AccountSpecialKaipingReception();
            accountSpecialKaipingReception.setKaipingguyId(specialByKaipingReception.getKaipingguyId());
            accountSpecialKaipingReception.setKaipingOrderIdList(accountSpecialKaipingOrderIdDetails);
            accountSpecialKaipingReception.setKaipingguyName(specialByKaipingReception.getKaipingguyName());
            if (Objects.equals(specialByKaipingReception.getRemark(), "")) {

                specialByKaipingReception.setRemark(null);
            }
            accountSpecialKaipingReception.setRemark(specialByKaipingReception.getRemark());
            accountSpecialKaipingReception.setPayOrTakeWay(specialByKaipingReception.getPayOrTakeWay());
            accountSpecialKaipingReception.setPayTime(specialByKaipingReception.getPayTime());
            accountSpecialKaipingReception.setShouldGiveMoney(specialByKaipingReception.getDebt());
            accountSpecialKaipingReception.setPayMoney(specialByKaipingReception.getPayMoney());
            accountSpecialKaipingReception.setIsOver(specialByKaipingReception.getIsOver());
            return  accountService.postAccountSpecialKaiping(accountSpecialKaipingReception,employeeId);
        }

    }

    @Override
    public ReturnMsg postInvoiceKaiingGuy(SpecialInvoiceByKaipingReception specialInvoiceByKaipingReception, Integer employeeId) {
        ReturnMsg msg = new ReturnMsg();
        List<HashMap<String, Object>> hashMaps = kaipingOrderMapper.selectAllBuKaipingguyIdInvoice(specialInvoiceByKaipingReception.getKaipingguyId());
        if (hashMaps.isEmpty()){
            Invoice invoice = new Invoice();
            invoice.setInvoiceId(LshUtil.getInvoiceKaiping());
            invoice.setInvoiceType("开平开票(特殊)");
            invoice.setEmployeeId(employeeId);
            invoice.setOperateTime(new Timestamp(new Date().getTime()));
            invoice.setInvoiceTime(specialInvoiceByKaipingReception.getInvoiceTime());
            invoice.setInvoiceMoney(specialInvoiceByKaipingReception.getInvoiceMoney());
            invoice.setWho(specialInvoiceByKaipingReception.getKaipingguyName());
            if (Objects.equals(specialInvoiceByKaipingReception.getRemark(), "")){
                specialInvoiceByKaipingReception.setRemark(null);
            }
            invoice.setRemark(specialInvoiceByKaipingReception.getRemark());
            if (Objects.equals(specialInvoiceByKaipingReception.getInvoiceTitle(),"")){
                specialInvoiceByKaipingReception.setInvoiceTitle(null);
            }
            invoice.setInvoiceTitle(specialInvoiceByKaipingReception.getInvoiceTitle());
            int insert = invoiceMapper.insert(invoice);
            QueryWrapper<KaipingGuy> kaipingGuyQueryWrapper = new QueryWrapper<>();
            kaipingGuyQueryWrapper.eq("kaipingguy_id",specialInvoiceByKaipingReception.getKaipingguyId());
            KaipingGuy kaipingGuy = kaipingGuyMapper.selectList(kaipingGuyQueryWrapper).get(0);
            if (specialInvoiceByKaipingReception.getIsOver()){
                    kaipingGuy.setInvoice(new BigDecimal("0"));
            }else {
                kaipingGuy.setInvoice(kaipingGuy.getInvoice().subtract(specialInvoiceByKaipingReception.getInvoiceMoney()));

            }
            int i = kaipingGuyMapper.updateById(kaipingGuy);
            if (insert==1&&i==1){
                msg.setCode(1);
                msg.setMsg("销售开票操作成功");

            }else {
                msg.setCode(0);
                msg.setMsg("销售开票操作失败");
            }
            return msg;
        }else {
            ArrayList<AccountSpecialKaipingOrderIdDetail> accountSpecialKaipingOrderIdDetails = new ArrayList<>();
            for (int i = 0; i <hashMaps.size() ; i++) {
                HashMap<String, Object> hashMap = hashMaps.get(i);
                String kaiping_order_id = hashMap.get("kaiping_order_id").toString();
                BigInteger bigInteger = new BigInteger(kaiping_order_id);
                java.sql.Date processing_time = (java.sql.Date)hashMap.get("processing_time");
                AccountSpecialKaipingOrderIdDetail accountSpecialKaipingOrderIdDetail = new AccountSpecialKaipingOrderIdDetail(bigInteger, processing_time);
                accountSpecialKaipingOrderIdDetails.add(accountSpecialKaipingOrderIdDetail);
            }
            InvoiceByKaipingOrder invoiceByKaipingOrder = new InvoiceByKaipingOrder();
            if (Objects.equals(specialInvoiceByKaipingReception.getRemark(), "")){
                specialInvoiceByKaipingReception.setRemark(null);
            }
            if (Objects.equals(specialInvoiceByKaipingReception.getInvoiceTitle(),"")){
                specialInvoiceByKaipingReception.setInvoiceTitle(null);
            }
            invoiceByKaipingOrder.setRemark(specialInvoiceByKaipingReception.getRemark());
            invoiceByKaipingOrder.setKaipingguyId(specialInvoiceByKaipingReception.getKaipingguyId());
            invoiceByKaipingOrder.setKaipingguyName(specialInvoiceByKaipingReception.getKaipingguyName());
            invoiceByKaipingOrder.setKaipingOrderIdList(accountSpecialKaipingOrderIdDetails);
            invoiceByKaipingOrder.setShouldInvoiceMoney(specialInvoiceByKaipingReception.getNoInvoice());
            invoiceByKaipingOrder.setInvoiceMoney(specialInvoiceByKaipingReception.getInvoiceMoney());
            invoiceByKaipingOrder.setIsOver(specialInvoiceByKaipingReception.getIsOver());
            invoiceByKaipingOrder.setInvoiceTime(specialInvoiceByKaipingReception.getInvoiceTime());
            invoiceByKaipingOrder.setInvoiceTitle(specialInvoiceByKaipingReception.getInvoiceTitle());

            return invoiceService.invoiceKaiping(invoiceByKaipingOrder,employeeId);





        }
    }
}
